package com.ld.common.webnotice.socket;

import java.util.Map;

import com.corundumstudio.socketio.SocketIOClient;
import com.ld.admin.webnotice.model.SocketIOClientInfo;
import com.ld.common.webnotice.model.PushMessage;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.reflect.ModelUtil;
import com.ld.shieldsb.common.core.util.useragent.UserAgentModel;
import com.ld.shieldsb.common.core.util.useragent.UserAgentUtil;

public class SocketIOServerUtil {
    public static final SocketIOServiceImpl SOCKET_IO_SERVICE = new SocketIOServiceImpl();

    /**
     * 获取客户端信息
     * 
     * @Title getClientMap
     * @author 吕凯
     * @date 2019年7月11日 上午11:49:00
     * @return Map<String,Map<String,SocketIOClient>>
     */
    public static Map<String, Map<String, SocketIOClient>> getClientMap() {
        return SocketIOServiceImpl.getClientMap();
    }

    public static void disconnected(SocketIOClient client) {
        PushMessage outline = new PushMessage();
        outline.setMessage("强制下线");
        SOCKET_IO_SERVICE.pushMessageToUser(client, outline);
        client.disconnect();
    }

    /**
     * 强制下线
     * 
     * @Title disconnected
     * @author 吕凯
     * @date 2019年7月12日 上午8:29:22
     * @param id
     *            uid_sessionId
     * @return Result
     */
    public static Result disconnected(String id) {
        Result result = new Result();
        result.setMessage("客户端不存在！");
        SocketIOClient client = getClientById(id);
        if (client != null) {
            PushMessage outline = new PushMessage();
            outline.setMessage("被管理员强制下线！");
            SOCKET_IO_SERVICE.pushMessageToUser(client, outline);
            client.disconnect(); // 会自动触发disconnect监听
            result.setMessage("强制下线成功！");
            result.setSuccess(true);
        }
        return result;

    }

    /**
     * 
     * 发送消息给具体的客户端
     * 
     * @Title sendMsg2Client
     * @author 吕凯
     * @date 2019年7月15日 下午5:39:40
     * @param fromUserId
     *            发送人id
     * @param fromUserName
     *            发送人姓名
     * @param id
     *            客户端的sessionId
     * @param message
     *            发送的消息
     * @return Result
     */
    public static Result sendMsg2Client(String fromUserId, String fromUserName, String id, String message) {
        Result result = new Result();
        result.setMessage("客户端不存在！");
        SocketIOClient client = getClientById(id);
        if (client != null) {
            PushMessage outline = new PushMessage();
            outline.setFromUserId(fromUserId);
            outline.setFromUserName(fromUserName);
            outline.setMessage(message);
            SOCKET_IO_SERVICE.pushMessageToUser(client, outline);
            result.setMessage("发送成功！");
            result.setSuccess(true);
        }
        return result;

    }

    /**
     * 根据uid_sessionId获取客户端对象
     * 
     * @Title getClientById
     * @author 吕凯
     * @date 2019年7月12日 上午9:38:16
     * @param id
     * @return SocketIOClient
     */
    public static SocketIOClient getClientById(String id) {
        SocketIOClient client = null;
        String[] uids = id.split("_");
        if (uids.length == 2) {
            String uid = uids[0];
            String sessionId = uids[1];
            Map<String, Map<String, SocketIOClient>> clientMap = SocketIOServerUtil.getClientMap();
            if (clientMap != null && !clientMap.isEmpty()) {
                Map<String, SocketIOClient> map = clientMap.get(uid);
                if (map != null && !map.isEmpty()) {
                    client = map.get(sessionId);
                }
            }
        }
        return client;
    }

    /**
     * 填充客户端信息
     * 
     * @Title fillInfo
     * @author 吕凯
     * @date 2019年7月11日 下午3:30:39
     * @param clientInfo
     * @param uid
     * @param sessionId
     * @param map
     *            void
     */
    public static void fillClientInfo(SocketIOClientInfo clientInfo, String uid, String sessionId, SocketIOClient client) {
        clientInfo.setUid(uid);
        clientInfo.setSessionId(sessionId);
        UserAgentModel userAgentModel = UserAgentUtil.getUserAgent(client.getHandshakeData().getHttpHeaders().get("User-Agent"));
        if (userAgentModel != null) {
            clientInfo.setClientInfo(
                    userAgentModel.getBrowserType() + " " + ModelUtil.getValueOrDefault(userAgentModel.getBrowserVersion(), "") + "【"
                            + userAgentModel.getPlatformType() + " " + userAgentModel.getPlatformSeries() + " "
                            + ModelUtil.getValueOrDefault(userAgentModel.getPlatformVersion(), "") + "】");
        }
        clientInfo.setLastLoginTime(client.getHandshakeData().getTime());
        clientInfo.setClient(client);
    }

}